package common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;



import math.KurtosisBasedMethod;
import math.LeastAbsMethod;
import math.LeastSquareMethod;
import math.WLeastAbsMethod;
import math.WLeastSquareMethod;
import math.common.MathCommon;
import math.probability.CompDistribution;
import math.probability.ExpDistribution;
import math.probability.LNormalDistribution;
import math.probability.LaplasDistribution;
import math.probability.NormalDistribution;
import math.probability.ProbDistribution;
import math.probability.UniformDistribution;





public class Test {
	ArrayList<Double> U; 
	ArrayList<Double> V;
	double meanU; 
	double meanV;

	
	private void modelData(ProbDistribution distX, ProbDistribution distKsi, 
			ProbDistribution distEps, int N, Double b0, Double b1, Random r) {
		U = new ArrayList<Double>(); 
		V = new ArrayList<Double>();
		ArrayList<Double> smplX = distX.getSample(N,r);
		ArrayList<Double> ksi = distKsi.getSample(N,r);
		ArrayList<Double> eps = distEps.getSample(N,r);
		double sumU = 0.0;
		double sumV = 0.0;
		for (int i = 0; i < N; i++) {
			V.add(b0+b1*smplX.get(i) + eps.get(i));
			U.add(smplX.get(i) + ksi.get(i));
			sumU += U.get(i);
			sumV += V.get(i);
		}
		meanU = sumU/N; 
		meanV = sumV/N;
		for (int i = 0; i < N; i++) {
			V.set(i, V.get(i)-meanV); 
			U.set(i, U.get(i)-meanU);
		}
		
	}

	private void modelData(ProbDistribution distX, ProbDistribution distKsi, 
			ProbDistribution distEps, int N, Double b0, Double b1) {
		U = new ArrayList<Double>(); 
		V = new ArrayList<Double>();
		ArrayList<Double> smplX = distX.getSample(N);
		ArrayList<Double> ksi = distKsi.getSample(N);
		ArrayList<Double> eps = distEps.getSample(N);
		double sumU = 0.0;
		double sumV = 0.0;
		for (int i = 0; i < N; i++) {
			V.add(b0+b1*smplX.get(i) + eps.get(i));
			U.add(smplX.get(i) + ksi.get(i));
			sumU += U.get(i);
			sumV += V.get(i);
		}
		meanU = sumU/N; 
		meanV = sumV/N;
		for (int i = 0; i < N; i++) {
			V.set(i, V.get(i)-meanV); 
			U.set(i, U.get(i)-meanU);
		}
		
	}
	
	public void doTestModelData() {
			int n=500;
			int M=10000;
			ProbDistribution distX = new ExpDistribution(1.0);
			ProbDistribution distEps = new CompDistribution(0.05, new NormalDistribution(0.0,0.55), 
					new NormalDistribution(5.0,1.0));
			ProbDistribution distKsi = new NormalDistribution(0.0, 2.0);
			modelData(distX, distKsi, distEps, n, 1.05, 2.04);
			
			Double sumMU41 = 0.0;
			Double sumMU42 = 0.0;
			
			Random r = new Random(0);
			for (int i=0; i<5; i++) {
//				Random r = new Random(0);
				System.out.println(r.nextDouble());
			}

	}
	
	
	public void doTest() throws FileNotFoundException {
		int N = 50;
		int M = 1000;
		Double b0 = 1.456;
		Double b1 = 10.0;
		ProbDistribution distX = new ExpDistribution(0.75);
		ProbDistribution distEps = new CompDistribution(0.05, new NormalDistribution(0.0,0.55), 
				new NormalDistribution(5.0,1.0));
		ProbDistribution distKsi = new NormalDistribution(0.0, 0.0);

		//modelData (new NormalDistribution(), new ExpDistribution(2.0), 
		//		new NormalDistribution(0.0, 0.5), N, b0, b1);
		
		ArrayList<Double> b11 = new ArrayList<Double>();
		ArrayList<Double> b12 = new ArrayList<Double>();
		ArrayList<Double> b13 = new ArrayList<Double>();
		ArrayList<Double> b14 = new ArrayList<Double>();
		ArrayList<Double> b01 = new ArrayList<Double>();
		ArrayList<Double> b02 = new ArrayList<Double>();
		ArrayList<Double> b03 = new ArrayList<Double>();
		ArrayList<Double> b04 = new ArrayList<Double>();
				
		for (int j=0; j<M; j++) {
			modelData (distX,distKsi,distEps, N, b0, b1);
			b11.add(new LeastSquareMethod().estimate(V, U));
			b01.add(meanV - b11.get(j) * meanU);
			b12.add(new WLeastSquareMethod().estimate(V, U));
			b02.add(meanV - b12.get(j) * meanU);
			b13.add(new LeastAbsMethod().estimate(V, U));
			b03.add(meanV - b13.get(j) * meanU);
			b14.add(new WLeastAbsMethod().estimate(V, U));
			b04.add(meanV - b14.get(j) * meanU);
		}
		int p5 =  new Double(M * 0.05).intValue();
		int p95 = new Double(M * 0.95).intValue();
		Collections.sort(b11);
		Collections.sort(b12);
		Collections.sort(b13);
		Collections.sort(b14);
		Collections.sort(b01);
		Collections.sort(b02);
		Collections.sort(b03);
		Collections.sort(b04);
		
		PrintWriter out = new PrintWriter(new FileOutputStream("estTesting.csv", true));
		out.println("");
		out.println("Testing");
		out.printf("N, %6d, M, %6d\n",N,M);
		out.printf("b0, %2.4f, b1, %2.4f\n",b0,b1);
		out.println("X," + distX.toString() + ",Ksi," + distKsi.toString());
		out.println("Eps," + distEps.toString());
		out.println("Results on b1");
		out.println("Method, b1 in, value, int length");
		out.printf("LSM,(%2.4f;%2.4f), %2.4f, %2.4f\n",b11.get(p5),b11.get(p95),
				(b11.get(p5)+b11.get(p95))/2, b11.get(p95)-b11.get(p5));
		out.printf("WLSM, (%2.4f;%2.4f), %2.4f, %2.4f\n",b12.get(p5),b12.get(p95),
				(b12.get(p5)+b12.get(p95))/2, b12.get(p95)-b12.get(p5));
		out.printf("LAM,(%2.4f;%2.4f), %2.4f, %2.4f\n",b13.get(p5),b13.get(p95),
				(b13.get(p5)+b13.get(p95))/2, b13.get(p95)-b13.get(p5));
		out.printf("WLAM, (%2.4f;%2.4f), %2.4f, %2.4f\n",b14.get(p5),b14.get(p95),
				(b14.get(p5)+b14.get(p95))/2, b14.get(p95)-b14.get(p5));
		
		out.println("Results on b0");
		out.println("Method, b0 in, value, int length");
		out.printf("LSM,(%2.4f;%2.4f), %2.4f, %2.4f\n",b01.get(p5),b01.get(p95),
				(b01.get(p5)+b01.get(p95))/2, b01.get(p95)-b01.get(p5));
		out.printf("WLSM, (%2.4f;%2.4f), %2.4f, %2.4f\n",b02.get(p5),b02.get(p95),
				(b02.get(p5)+b02.get(p95))/2, b02.get(p95)-b02.get(p5));
		out.printf("LAM,(%2.4f;%2.4f), %2.4f, %2.4f\n",b03.get(p5),b03.get(p95),
				(b03.get(p5)+b03.get(p95))/2, b03.get(p95)-b03.get(p5));
		out.printf("WLAM, (%2.4f;%2.4f), %2.4f, %2.4f\n",b04.get(p5),b04.get(p95),
				(b04.get(p5)+b04.get(p95))/2, b04.get(p95)-b04.get(p5));
		
		
		out.flush();
		out.close();
		

		
	}
	
	public void doTestEstimationBias() throws IOException {
		int N = 500;
		int M = 1000;
		Double b0 = 0.5;
		Double b1 = 1.05;
		String fileName = "test_N"+N+"M"+M+".csv";
//		ProbDistribution distX = new LNormalDistribution(1.0,2.0);
		ProbDistribution distX = new ExpDistribution(1/Math.sqrt(2.0));
//		ProbDistribution distEps = new CompDistribution(0.05, new NormalDistribution(0.0,0.55), 
//				new NormalDistribution(5.0,1.0));
		ProbDistribution distEps = new NormalDistribution(0.0,5.0);
//		ProbDistribution distEps = new CompDistribution(0.3, new NormalDistribution(0.0,0.55), 
//				new ExpDistribution(0.5));
		ProbDistribution distKsi = new NormalDistribution(0.0, 0.5);
//		ProbDistribution distKsi = new CompDistribution(0.15, new NormalDistribution(0.0,0.4), 
//				new NormalDistribution(0.0,2.0));
		
		ArrayList<Double> b11 = new ArrayList<Double>();
		ArrayList<Double> b12 = new ArrayList<Double>();
		ArrayList<Double> b13 = new ArrayList<Double>();
		ArrayList<Double> b14 = new ArrayList<Double>();
		ArrayList<Double> b01 = new ArrayList<Double>();
		ArrayList<Double> b02 = new ArrayList<Double>();
		ArrayList<Double> b03 = new ArrayList<Double>();
		ArrayList<Double> b04 = new ArrayList<Double>();
		
		Random r = new Random(0);
		for (int j=0; j<M; j++) {
			modelData (distX,distKsi,distEps, N, b0, b1,r);
			b11.add(new LeastSquareMethod().estimate(V, U));
			b01.add(meanV - b11.get(j) * meanU);
			b12.add(new WLeastSquareMethod().estimate(V, U));
			b02.add(meanV - b12.get(j) * meanU);
			b13.add(new LeastAbsMethod().estimate(V, U));
			b03.add(meanV - b13.get(j) * meanU);
			b14.add(new WLeastAbsMethod().estimate(V, U));
			b04.add(meanV - b14.get(j) * meanU);
			
			
		}
		int p5 =  new Double(M * 0.05).intValue();
		int p95 = new Double(M * 0.95).intValue();
		Collections.sort(b11);
		Collections.sort(b12);
		Collections.sort(b13);
		Collections.sort(b14);
		Collections.sort(b01);
		Collections.sort(b02);
		Collections.sort(b03);
		Collections.sort(b04);
		Double mb11 = (b11.get(p5)+b11.get(p95))/2;
		Double mb01 = (b01.get(p5)+b01.get(p95))/2;
		Double mb12 = (b12.get(p5)+b12.get(p95))/2;
		Double mb02 = (b02.get(p5)+b02.get(p95))/2;
		Double mb13 = (b13.get(p5)+b13.get(p95))/2;
		Double mb03 = (b03.get(p5)+b03.get(p95))/2;
		Double mb14 = (b14.get(p5)+b14.get(p95))/2;
		Double mb04 = (b04.get(p5)+b04.get(p95))/2;
		
		boolean emptyFile = true;
		try {
		FileInputStream file = new FileInputStream(fileName);
		if (file.read() != -1) 
			emptyFile = false;
		file.close();
		} catch (IOException e) 
		{}
		
		PrintWriter out = new PrintWriter(new FileOutputStream(fileName, true));
		if (emptyFile)  
			out.println("X,ksi,eps,Method, b1, (b1-dlt;b1+dlt), dlt(%), b1 err, b0, (b0-dlt;b0+dlt), dlt(%), b0 err");
		
		out.print(distX.toString()+","+ distKsi.toString()+","+distEps.toString()+",");
		
		out.printf("LSM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n", 
				mb11, b11.get(p5),b11.get(p95), ((b11.get(p95)-b11.get(p5))/2/mb11)*100, mb11/b1, 
				mb01, b01.get(p5),b01.get(p95), ((b01.get(p95)-b01.get(p5))/2/mb01)*100, mb01/b0);
		
		out.printf(",,,LAM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n", 
				mb13, b13.get(p5), b13.get(p95), ((b13.get(p95)-b13.get(p5))/2/mb13)*100, mb13/b1, 
				mb03, b03.get(p5), b03.get(p95), ((b03.get(p95)-b03.get(p5))/2/mb03)*100, mb03/b0);
		
		out.printf(",,,WLSM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n", 
				mb12, b12.get(p5), b12.get(p95), ((b12.get(p95)-b12.get(p5))/2/mb12)*100, mb12/b1, 
				mb02, b02.get(p5), b02.get(p95), ((b02.get(p95)-b02.get(p5))/2/mb02)*100, mb02/b0);
		
		out.printf(",,,WLAM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n", 
				mb14, b14.get(p5), b14.get(p95), ((b14.get(p95)-b14.get(p5))/2/mb14)*100, mb14/b1, 
				mb04, b04.get(p5), b04.get(p95), ((b04.get(p95)-b04.get(p5))/2/mb04)*100, mb04/b0);
		
		out.flush();
		out.close();
	}
	
	public void doTestEstimationBias(PrintWriter out, ProbDistribution distX, ProbDistribution distKsi, ProbDistribution distEps) throws IOException {
		int N = 500;
		int M = 1000;
		Double b0 = 0.5;
		Double b1 = 1.05;
		
		ArrayList<Double> b11 = new ArrayList<Double>();
		ArrayList<Double> b12 = new ArrayList<Double>();
		ArrayList<Double> b13 = new ArrayList<Double>();
		ArrayList<Double> b14 = new ArrayList<Double>();
		ArrayList<Double> b01 = new ArrayList<Double>();
		ArrayList<Double> b02 = new ArrayList<Double>();
		ArrayList<Double> b03 = new ArrayList<Double>();
		ArrayList<Double> b04 = new ArrayList<Double>();
		
		Random r = new Random(0);
		for (int j=0; j<M; j++) {
			modelData (distX,distKsi,distEps, N, b0, b1,r);
			b11.add(new LeastSquareMethod().estimate(V, U));
			b01.add(meanV - b11.get(j) * meanU);
			b12.add(new WLeastSquareMethod().estimate(V, U));
			b02.add(meanV - b12.get(j) * meanU);
			b13.add(new LeastAbsMethod().estimate(V, U));
			b03.add(meanV - b13.get(j) * meanU);
			b14.add(new WLeastAbsMethod().estimate(V, U));
			b04.add(meanV - b14.get(j) * meanU);
			
			
		}
		int p5 =  new Double(M * 0.05).intValue();
		int p95 = new Double(M * 0.95).intValue();
		Collections.sort(b11);
		Collections.sort(b12);
		Collections.sort(b13);
		Collections.sort(b14);
		Collections.sort(b01);
		Collections.sort(b02);
		Collections.sort(b03);
		Collections.sort(b04);
		Double mb11 = (b11.get(p5)+b11.get(p95))/2;
		Double mb01 = (b01.get(p5)+b01.get(p95))/2;
		Double mb12 = (b12.get(p5)+b12.get(p95))/2;
		Double mb02 = (b02.get(p5)+b02.get(p95))/2;
		Double mb13 = (b13.get(p5)+b13.get(p95))/2;
		Double mb03 = (b03.get(p5)+b03.get(p95))/2;
		Double mb14 = (b14.get(p5)+b14.get(p95))/2;
		Double mb04 = (b04.get(p5)+b04.get(p95))/2;
		
//		boolean emptyFile = true;
//		try {
//		FileInputStream file = new FileInputStream(fileName);
//		if (file.read() != -1) 
//			emptyFile = false;
//		file.close();
//		} catch (IOException e) 
//		{}
//		
//		PrintWriter out = new PrintWriter(new FileOutputStream(fileName, true));
//		if (emptyFile)  
//			out.println("s2X,s2Ksi,s2Eps,Method, b1, (b1-dlt;b1+dlt), dlt(%), b1 err, b0, (b0-dlt;b0+dlt), dlt(%), b0 err");
//		
		
		
//		out.printf("%2.4f, %2.4f, %2.4f,LSM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n",
//				distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
//				mb11, b11.get(p5),b11.get(p95), ((b11.get(p95)-b11.get(p5))/2/mb11)*100, mb11/b1, 
//				mb01, b01.get(p5),b01.get(p95), ((b01.get(p95)-b01.get(p5))/2/mb01)*100, mb01/b0);
//		
//		out.printf("%2.4f, %2.4f, %2.4f,LAM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n",
//				distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
//				mb13, b13.get(p5), b13.get(p95), ((b13.get(p95)-b13.get(p5))/2/mb13)*100, mb13/b1, 
//				mb03, b03.get(p5), b03.get(p95), ((b03.get(p95)-b03.get(p5))/2/mb03)*100, mb03/b0);
//		
//		out.printf("%2.4f, %2.4f, %2.4f,WLSM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n",
//				distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
//				mb12, b12.get(p5), b12.get(p95), ((b12.get(p95)-b12.get(p5))/2/mb12)*100, mb12/b1, 
//				mb02, b02.get(p5), b02.get(p95), ((b02.get(p95)-b02.get(p5))/2/mb02)*100, mb02/b0);
//		
//		out.printf("%2.4f, %2.4f, %2.4f,WLAM,%2.4f,(%2.4f;%2.4f), %2.4f, %2.4f, %2.4f, (%2.4f;%2.4f), %2.4f, %2.4f\n",
//				distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
//				mb14, b14.get(p5), b14.get(p95), ((b14.get(p95)-b14.get(p5))/2/mb14)*100, mb14/b1, 
//				mb04, b04.get(p5), b04.get(p95), ((b04.get(p95)-b04.get(p5))/2/mb04)*100, mb04/b0);
		out.printf("%2.4f, %2.4f, %2.4f, b1, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f," +
				" %3.4f, %3.4f, %3.4f, %3.4f\n", distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
				mb11, mb13, mb12, mb14, mb11/b1, mb13/b1, mb12/b1, mb14/b1, ((b11.get(p95)-b11.get(p5))/2/mb11)*100, 
				((b13.get(p95)-b13.get(p5))/2/mb13)*100, ((b12.get(p95)-b12.get(p5))/2/mb12)*100, ((b14.get(p95)-b14.get(p5))/2/mb14)*100);
		out.printf("%2.4f, %2.4f, %2.4f, b0, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f," +
				" %3.4f, %3.4f, %3.4f, %3.4f\n", distX.getDisp(), distKsi.getDisp(), distEps.getDisp(),
				mb01, mb03, mb02, mb04, mb01/b0, mb03/b0, mb02/b0, mb04/b0, ((b01.get(p95)-b01.get(p5))/2/mb01)*100, 
				((b03.get(p95)-b03.get(p5))/2/mb03)*100, ((b02.get(p95)-b02.get(p5))/2/mb02)*100, ((b04.get(p95)-b04.get(p5))/2/mb04)*100);
		
		
//		out.flush();
//		out.close();
	}
	
	
	public void doTest1(String fileName) throws FileNotFoundException {
		int N = 500;
		int M = 1000;
		Double b0 = 0.5;
		Double b1 = 1.05;
//		ProbDistribution distX = new LNormalDistribution(1.0,0.5);
		ProbDistribution distX = new ExpDistribution(0.75);
//		ProbDistribution distEps = new CompDistribution(0.05, new NormalDistribution(0.0,0.55), 
//				new NormalDistribution(5.0,1.0));
		ProbDistribution distEps = new NormalDistribution(0.0,0.5);
//		ProbDistribution distEps = new CompDistribution(0.3, new NormalDistribution(0.0,0.55), 
//				new ExpDistribution(0.5));
		ProbDistribution distKsi = new NormalDistribution(0.0, 0.4);
//		ProbDistribution distKsi = new CompDistribution(0.15, new NormalDistribution(0.0,0.4), 
//				new NormalDistribution(0.0,2.0));
		
		ArrayList<Double> b11 = new ArrayList<Double>();
		ArrayList<Double> b12 = new ArrayList<Double>();
		ArrayList<Double> b13 = new ArrayList<Double>();
		ArrayList<Double> b14 = new ArrayList<Double>();
		ArrayList<Double> b15 = new ArrayList<Double>();
		ArrayList<Double> b01 = new ArrayList<Double>();
		ArrayList<Double> b02 = new ArrayList<Double>();
		ArrayList<Double> b03 = new ArrayList<Double>();
		ArrayList<Double> b04 = new ArrayList<Double>();
		ArrayList<Double> b05 = new ArrayList<Double>();
		
		Random r = new Random(0);
		for (int j=0; j<M; j++) {
			modelData (distX,distKsi,distEps, N, b0, b1,r);
			b11.add(new LeastSquareMethod().estimate(V, U));
			b01.add(meanV - b11.get(j) * meanU);
			b12.add(new WLeastSquareMethod().estimate(V, U));
			b02.add(meanV - b12.get(j) * meanU);
			b13.add(new LeastAbsMethod().estimate(V, U));
			b03.add(meanV - b13.get(j) * meanU);
			b14.add(new WLeastAbsMethod().estimate(V, U));
			b04.add(meanV - b14.get(j) * meanU);
			b15.add(new KurtosisBasedMethod().estimate(V, U));
			b05.add(meanV - b15.get(j) * meanU);
			
		}
		int p5 =  new Double(M * 0.05).intValue();
		int p95 = new Double(M * 0.95).intValue();
		Collections.sort(b11);
		Collections.sort(b12);
		Collections.sort(b13);
		Collections.sort(b14);
		Collections.sort(b15);
		Collections.sort(b01);
		Collections.sort(b02);
		Collections.sort(b03);
		Collections.sort(b04);
		Collections.sort(b05);
		
		PrintWriter out = new PrintWriter(new FileOutputStream(fileName, true));
		out.println("");
		out.println("Testing");
		out.printf("N, %6d, M, %6d\n",N,M);
		out.printf("b0, %2.4f, b1, %2.4f\n",b0,b1);
		out.println("X," + distX.toString() + ",Ksi," + distKsi.toString());
		out.println("Eps," + distEps.toString());
		out.println("Results");
		out.println("Method, b1, b1 95% c.i., c.i. length, b0,  b0 95% c.i., c.i. length");
		
		out.printf("LSM,%2.4f,(%2.4f;%2.4f), %2.4f,%2.4f,(%2.4f;%2.4f), %2.4f\n", 
				(b11.get(p5)+b11.get(p95))/2, b11.get(p5),b11.get(p95), b11.get(p95)-b11.get(p5),
				(b01.get(p5)+b01.get(p95))/2, b01.get(p5),b01.get(p95),b01.get(p95)-b01.get(p5));
		
		out.printf("WLSM,%2.4f,(%2.4f;%2.4f), %2.4f,%2.4f,(%2.4f;%2.4f), %2.4f\n", 
				(b12.get(p5)+b12.get(p95))/2, b12.get(p5),b12.get(p95), b12.get(p95)-b12.get(p5),
				(b02.get(p5)+b02.get(p95))/2, b02.get(p5),b02.get(p95), b02.get(p95)-b02.get(p5));
		out.printf("LAM,%2.4f,(%2.4f;%2.4f), %2.4f,%2.4f,(%2.4f;%2.4f), %2.4f\n", 
				(b13.get(p5)+b13.get(p95))/2, b13.get(p5),b13.get(p95), b13.get(p95)-b13.get(p5),
				(b03.get(p5)+b03.get(p95))/2, b03.get(p5),b03.get(p95), b03.get(p95)-b03.get(p5));
		
		out.printf("WLAM, %2.4f,(%2.4f;%2.4f), %2.4f,%2.4f,(%2.4f;%2.4f), %2.4f\n", 
				(b14.get(p5)+b14.get(p95))/2, b14.get(p5),b14.get(p95), b14.get(p95)-b14.get(p5),
				(b04.get(p5)+b04.get(p95))/2, b04.get(p5),b04.get(p95), b04.get(p95)-b04.get(p5));
		out.printf("FullM, %2.4f,(%2.4f;%2.4f), %2.4f,%2.4f,(%2.4f;%2.4f), %2.4f\n", 
				(b15.get(p5)+b15.get(p95))/2, b15.get(p5),b15.get(p95), b15.get(p95)-b15.get(p5),
				(b05.get(p5)+b05.get(p95))/2, b05.get(p5),b05.get(p95), b05.get(p95)-b05.get(p5));
		
		out.flush();
		out.close();
		

		
	}
	
	public static void main(String arg[]) throws IOException  {
		Test t = new Test();
//		t.doTest1("EpsWithOutliers1.csv");
//		t.doTestModelData();
//		t.doTestEstimationBias();
		
		String fileName = "testX_Exp_Ksi_L_Eps_N.csv";
		Double maxS2X = 3.02;
		Double maxS2Ksi = 3.02;
		Double maxS2Eps = 3.02;

		
		
		PrintWriter out = new PrintWriter(new FileOutputStream(fileName, true));
//		out.println("s2X,s2Ksi,s2Eps,Method, b1, (b1-dlt;b1+dlt), dlt(%), b1 err, b0, (b0-dlt;b0+dlt), dlt(%), b0 err");
		out.println("s2X,s2Ksi,s2Eps, Coeff, LSM est, LAM est, WLSM est, WLAM est, LSM bias, LAM bias, WLSM bias, WLAM bias," +
				"LSM err, LAM err, WLSM err, WLAM err");
		
		for (Double sigma2Eps = 0.5; sigma2Eps < maxS2Eps; sigma2Eps+=0.25)  {
			System.out.println("Calculating sigma2Eps = " + sigma2Eps );
			for (Double sigma2Ksi = 0.5; sigma2Ksi < maxS2Ksi; sigma2Ksi+=0.25) {
				for (Double sigma2X = 0.5; sigma2X < maxS2X; sigma2X+=0.25) {
					ProbDistribution distX = new ExpDistribution(1/Math.sqrt(sigma2X));
//					ProbDistribution distEps = new CompDistribution(0.05, new NormalDistribution(0.0,0.55), 
//							new NormalDistribution(5.0,1.0));
					ProbDistribution distEps = new NormalDistribution(0.0,sigma2Eps);
//					ProbDistribution distEps = new CompDistribution(0.3, new NormalDistribution(0.0,0.55), 
//							new ExpDistribution(0.5));
//					ProbDistribution distKsi = new UniformDistribution(- Math.sqrt(12*sigma2Ksi)/2, Math.sqrt(12*sigma2Ksi)/2);
					ProbDistribution distKsi = new LaplasDistribution(Math.sqrt(2.0/sigma2Ksi), 0.0);
//					ProbDistribution distKsi = new NormalDistribution(0.0, sigma2Ksi);
//					ProbDistribution distKsi = new CompDistribution(0.15, new NormalDistribution(0.0,0.4), 
//							new NormalDistribution(0.0,2.0));
					t.doTestEstimationBias(out, distX, distKsi, distEps);
				}
				out.flush();
			}
			
		}
		out.close();
		
	}

}
